﻿namespace AddressBook.Business.Event
{
  #region "Using Statements"
  using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;

  using Csla;
  using Csla.Data;
  #endregion

  [Serializable]
  public class LocationList : ReadOnlyListBase<LocationList, LocationInfo>
  {
		#region  "Factory Methods"

    public static LocationList EmptyList()
    {
      return new LocationList();
    }

    public static LocationList GetLocationList()
    {
      return DataPortal.Fetch<LocationList>();
    }

		private LocationList()
    { /* require use of factory methods */ }

    #endregion

		#region  "Data Access"

		private void DataPortal_Fetch()
		{
			RaiseListChangedEvents = false;

			using (var ctx = ContextManager<AddressBook.Business.Model.AddressBookDataContext>.GetManager(AddressBook.Business.Model.Database.AddressBook))
			{
				List<LocationInfo> data = (from l in ctx.DataContext.Locations
																	orderby l.Name
																	select new LocationInfo(
																		l.Id,
																		l.Name
																		)).ToList<LocationInfo>();

				this.IsReadOnly = false;

				this.AddRange(data);

				this.IsReadOnly = true;
			}

			RaiseListChangedEvents = true;
		}

		#endregion
  }
}
